<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends CIC_A_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$this->admin_priv(__METHOD__);
		$data['category'] = $this->cic_model->get_all('category',array('category_pid'=>0),array('category_sort','asc','category_id','asc'));
		$this->load->view('admin/category/index',$data);
	}
	
	function category_move()
	{
		$data['category'] 		= $this->cic_model->get_one('category',array('category_id'=>$this->uri->segment(4)));
		$data['category_tree']  = $this->get_category_tree();
		$this->load->view('admin/category/category_move',$data);
	}
	
	function move_category()
	{
		$this->admin_priv(__METHOD__);
		if($this->cic_model->update('category',array('category_pid'=>$this->input->post('category_pid')),array('category_id'=>$this->input->post('category_id'))))
		{
			$this->message->msg('移动成功！',site_url('admin/category/index'));
		}else{
			$this->message->msg('移动失败！',site_url('admin/category/category_move/'.$this->input->post('category_id')));
		}
	}
	
	function category_add()
	{
		$data = array();
		if($this->uri->segment(4))
			$data['category'] = $this->cic_model->get_one('category',array('category_id'=>$this->uri->segment(4)));
		$data['model'] = $this->cic_model->get_all('model','',array('model_sort','asc'));
		$this->load->view('admin/category/category_add',$data);
	}
	function add_category()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('category', 'admin/category/category_add');
		$data = array(
				'category_pid'				=> $this->input->post('category_pid'),
				'category_name'				=> $this->input->post('category_name'),
				'model_id'					=> $this->input->post('model_id'),
				'category_display_top'		=> $this->input->post('category_display_top'),
				'category_display_bottom'	=> $this->input->post('category_display_bottom'),
				'category_tpl'				=> $this->input->post('category_tpl'),
				'category_tpl_out'			=> $this->input->post('category_tpl_out'),
				'category_open'				=> $this->input->post('category_open'),
				'category_sort'				=> $this->input->post('category_sort'),
				'tpl_list_num'				=> $this->input->post('tpl_list_num'),
				'tpl_list'					=> $this->input->post('tpl_list'),
				'tpl_content'				=> $this->input->post('tpl_content'),
		);
		if($this->cic_model->insert('category',$data))
		{
			$this->message->msg('添加成功！',site_url('admin/category/index'));
		}else{
			$this->message->msg('添加失败！',site_url('admin/category/category_add'));
		}
	}
	
	function category_edit()
	{
		$data = array();
		if($segment = $this->uri->segment(4))
			$data['category'] = $this->cic_model->get_one('category',array('category_id'=>$segment));
		$data['model'] = $this->cic_model->get_all('model','',array('model_sort','asc'));
		$this->load->view('admin/category/category_edit',$data);
	}
	function edit_category()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('category', 'admin/category/category_add');
		$data = array(
				'category_name'				=> $this->input->post('category_name'),
				'model_id'					=> $this->input->post('model_id'),
				'category_display_top'		=> $this->input->post('category_display_top'),
				'category_display_bottom'	=> $this->input->post('category_display_bottom'),
				'category_tpl'				=> $this->input->post('category_tpl'),
				'category_tpl_out'			=> $this->input->post('category_tpl_out'),
				'category_open'				=> $this->input->post('category_open'),
				'category_sort'				=> $this->input->post('category_sort'),
				'tpl_list_num'				=> $this->input->post('tpl_list_num'),
				'tpl_list'					=> $this->input->post('tpl_list'),
				'tpl_content'				=> $this->input->post('tpl_content'),
		);
		$result = $this->cic_model->update('category',$data,array('category_id'=>$this->uri->segment(4)));
		if($result)
		{
			$this->message->msg('修改成功！',site_url('admin/category/index'));
		}else{
			$this->message->msg('未做任何修改！',site_url('admin/category/category_edit/'.$this->uri->segment(4)));
		}
	}
	
	function delete_category()
	{
		$this->admin_priv(__METHOD__);
		$category_id = $this->input->post('category_id');
		$query = $this->cic_model->get_all('category',array('category_pid' => $category_id));
		if($query)
		{
			echo 2;			
		}else{
			$category = $this->cic_model->get_one('category',array('category_id' => $category_id));
			$this->delete_content($category->model_id, $category_id);
			if($this->cic_model->delete('category',array('category_id' => $category_id)))
			{
				echo 1;
			}
		}
	}
	
	private function delete_content($model_id,$category_id)
	{
		$model = $this->cic_model->get_one('model',array('model_id' => $model_id));
		if($model)
		{
			$this->cic_model->delete($model->model_table_name,array('category_id'=>$category_id));
		}
		
	}
	
	function get_children()
	{
		$query = $this->cic_model->get_all('category',array('category_pid'=>$this->input->post('category_id')),array('category_sort','asc'));
		if($query)
		{
			foreach ($query->result() as $key=>$value)
			{
				$data[$key]['category_id'] = $value->category_id;
				$data[$key]['category_pid'] = $value->category_pid;
				$data[$key]['category_name'] = $value->category_name;
				$data[$key]['model_id'] = $value->model_id;
				$data[$key]['category_display_top'] = $value->category_display_top;
				$data[$key]['category_display_bottom'] = $value->category_display_bottom;
				$data[$key]['category_tpl'] = $value->category_tpl;
				$data[$key]['category_tpl_out'] = $value->category_tpl_out;
				$data[$key]['category_open'] = $value->category_open;
				$data[$key]['category_sort'] = $value->category_sort;
				$data[$key]['tpl_list_num'] = $value->tpl_list_num;
				$data[$key]['tpl_list'] = $value->tpl_list;
				$data[$key]['tpl_content'] = $value->tpl_content;
			}
			echo json_encode($data);
		}else{
			echo 0;
		}
	}
}